﻿Imports System.Object
Imports VSModel
Imports Cls_Common
Imports System.IO
Imports System.Data
Partial Class FrontEnd_Control_TinMuaXe_TenMuaXeAtHome
    Inherits System.Web.UI.UserControl

#Region "Sub and function"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            LoadData()
        End If
    End Sub
    Private Sub LoadData()
        Using Data As New VSEntities
            'Dim p = (From q In Data.TinMuaXes Where q.StatusId = 3 And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) _
            '         Order By q.PublishedDate Descending _
            '         Select q.TieuDe, q.TinhThanhId, q.NoiDung).Take(8).ToList
            'rptShow.DataSource = p
            'rptShow.DataBind()


            Dim k As New List(Of VSModel.TinMuaXe)
            Dim p = (From q In Data.TinMuaXes Where q.StatusId = 3 And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) Select q).Take(5).ToList
            Static rnd As New Random
            For j = 1 To 5
                Dim r As Integer = rnd.Next(p.Count)
                'get item into k
                k.Add(p(r))
                p.RemoveAt(r)
            Next j
            With rptShow
                .DataSource = k
                .DataBind()
            End With

            '  hplMore.NavigateUrl = MainDomain & "rao-vat.html" '"{QueryString}-{Id}/trang-chu.html
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub rptShow_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptShow.ItemDataBound
        Dim row = CType(e.Item.DataItem, Object)

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        Dim ltrDes As Literal = CType(e.Item.FindControl("ltrDes"), Literal)
        hplTitle.Text = row.TieuDe & " -<span class='USDHome'>" & GetTinhThanhPho(row.TinhThanhId) & "</span>"
        hplTitle.NavigateUrl = MainDomain & "tin-mua-oto.html"
        ltrDes.Text = CropText(row.NoiDung, 200, "...")
    End Sub
    Private Function GetTinhThanhPho(ByVal _TinhThanhId As Integer) As String
        Using _data As New VSEntities
            Dim p = (From q In _data.DonViHanhChinhs Where q.Id = _TinhThanhId Select q.Title).FirstOrDefault
            If Not p Is Nothing Then
                Return p
            End If

        End Using
        Return ""
    End Function
#End Region
End Class
